#include "Class/Manager/Input.hpp"

#include <iostream>
namespace		Manager
{
  void					Input::Update(float time)
  {
	this->_UpdateStates(time);

	static sf::Event	event;

	while (this->_Graphics.GetEvent(event))
	  {
		if (event.Type == sf::Event::KeyPressed)
		  {
			this->_keyTime[event.Key.Code] = .0f;
			if (this->_keyState[event.Key.Code] != ::Input::Pressed)
			  this->_keyState[event.Key.Code] = ::Input::JustPressed;
		  }
		else if (event.Type == sf::Event::KeyReleased)
		  this->_keyState[event.Key.Code] = ::Input::JustReleased;

		else if (event.Type == sf::Event::MouseButtonPressed)
		  {
			this->_mouseTime[event.MouseButton.Button] = .0f;
			if (this->_mouseState[event.MouseButton.Button] != ::Input::Pressed)
			  this->_mouseState[event.MouseButton.Button] = ::Input::JustPressed;
			this->_mousePos[Math::X] = event.MouseMove.X;
			this->_mousePos[Math::Y] = event.MouseMove.Y;
		  }
		else if (event.Type == sf::Event::MouseButtonReleased)
		  {
			this->_mouseState[event.MouseButton.Button] = ::Input::JustReleased;
			this->_mousePos[Math::X] = event.MouseMove.X;
			this->_mousePos[Math::Y] = event.MouseMove.Y;
		  }

		else if (event.Type == sf::Event::MouseMoved)
		  {
			this->_mousePos[Math::X] = event.MouseMove.X;
			this->_mousePos[Math::Y] = event.MouseMove.Y;
		  }

		else if (event.Type == sf::Event::MouseWheelMoved)
		  this->_mouseDelta = event.MouseWheel.Delta;
	  }
  }

  void					Input::_UpdateStates(float time)
  {
	static int unsigned	kc = ::Input::Key::Count, mc = ::Input::Mouse::ButtonCount;

	for (int unsigned i = 0 ; i != kc ; ++i)
	  {
		if (this->_keyState[i] == ::Input::JustPressed)
		  {
			this->_keyState[i] = ::Input::Pressed;
			this->_keyTime[i] = .0f;
		  }
		else if (this->_keyState[i] == ::Input::JustReleased)
		  {
			this->_keyState[i] = ::Input::NotPressed;
			this->_keyTime[i] = .0f;
		  }
		else if (this->_keyState[i] == ::Input::Pressed)
		  this->_keyTime[i] += time;
	  }

	for (int unsigned i = 0 ; i != mc ; ++i)
	  {
		if (this->_mouseState[i] == ::Input::JustPressed)
		  {
			this->_mouseState[i] = ::Input::Pressed;
			this->_mouseTime[i] = .0f;
		  }
		else if (this->_mouseState[i] == ::Input::JustReleased)
		  {
			this->_mouseState[i] = ::Input::NotPressed;
			this->_mouseTime[i] = .0f;
		  }
		else if (this->_mouseState[i] == ::Input::Pressed)
		  this->_mouseTime[i] += time;
	  }
  }
}
